//
//  extensionA.swift
//  swiftStudy
//
//  Created by unisedu on 16/4/1.
//  Copyright © 2016年 CaoFei. All rights reserved.
//

import UIKit

//在现有的类和结构体的基础上，扩展新的功能
//extension SomeTyep{
////new functionality to add to SomeType goes here
//}

//extension SomeType:SomeProtocol,AnotherProtocol{
//    //implementation of protocol requirements goes here
//}

//计算属性
extension Double {
    var km: Double {return self * 1_000.0}
    var m: Double {return self }
    var cm: Double {return self/100.0}
    var mm: Double {return self/1_000.0}
    var ft: Double {return self/3.28084}
}


//使用构造器
//struct Size {
//    var width = 0.0 ,height = 0.0
//}
//
//struct Point {
//    var x = 0.0 , y = 0.0
//}
//
//struct Rect {
//    var origin = Point ()
//    var size = Size ()
//}

extension Rect {
    
    init(center : Point , size: Size){
        let originX = center.x - (size.width/2)
        let originY = center.y - (size.height/2)
        //调用Rect 结构体构造器
        self.init(origin : Point (x:originX ,y:originY) , size : size)
        
    }
}

let centerRect = Rect (center:Point (x:4.0 ,y:4.0) , size : Size (width : 3.0 , height : 3.0))




//方法扩展
extension Int {
    func message() -> String {
        var message = ""
        switch self {
        case 0:
            message = "成功"
        case 0:
            message = "用户登录失败"
        default:
            message = "未知错误"
        }
        return message
        
    }
}





class extensionA: NSObject {
    func show(){
        let onelnch = 25.4.mm
        print ("One inch is \(onelnch) meters")
        
        let threeFeet = 3.ft
        print ("Three feet is \(threeFeet) meters")
        
        let aMarathon = 42.km + 195.m
        print ("A marathon is \(aMarathon) meters long")
        3.message()
        0.message()
        //负数要加括号
        (-1).message()
        

    }
}


